#!/usr/bin/env perl

use warnings;
use strict;

use lib './perl';
# JSON::Tiny is included in the "JSON" directory, so it doesn't need to be
# installed. JSON::Tiny is licensed under Artistic License version 2.0
# (http://www.perlfoundation.org/artistic_license_2_0) available from:
# https://metacpan.org/pod/JSON::Tiny
use JSON::Tiny qw(decode_json);

# For array_contains
use List::Util qw(any);

sub get_file_contents {
	my $filepath = shift;
	if ( -f $filepath ) {	
		open( my $file_handle, "<", "$filepath" ) or die("File $filepath not found");
		$/ = undef;
		my $content = <$file_handle>;
		close($file_handle);
		return $content;
	} else {
		return '';
	}
}

sub write_file_contents {
	my $string = shift;
	my $filepath = shift;

	open(my $file_handler, '>', $filepath) or die $!;
	print $file_handler $string;
	close($file_handler);
}

sub read_json {
	return decode_json( get_file_contents(shift) );
}

sub string_replace {
	my $string = shift;
	my $search = shift;
	my $replace = shift;
	$string =~ s/\Q$search\E/$replace/g;
	return $string;
}

# requires having use List::Util qw(any);
sub array_contains {
	my $value = shift;
	# https://stackoverflow.com/a/16690762
	return any { $_ eq $value } @_;
}

my $json = read_json('src/info.json');

# Get templates
my $html = get_file_contents('src/html/index.html');
my $menu_item_template = get_file_contents('src/html/menu_item.html');
my $column_headers_template = get_file_contents('src/html/column_headers.html');
my $code_rows_template = get_file_contents('src/html/code_rows.html');
my $code_cell_template = get_file_contents('src/html/code_cell.html');

my $output_queue;
my $output_queue2;
my $output;
my $output2;
my $temp;

if ( $json->{languages} ) {

	# Process menu items
	$output_queue = '';
	foreach my $lang (@{$json->{languages}}) {
		$output = $menu_item_template;
		if ( array_contains( $lang->{slug}, @{$json->{default_langs}} ) ) {
			$output = string_replace($output, '{{_ checked _}}', 'checked');
		} else {
			$output = string_replace($output, '{{_ checked _}}', '');
		}
		$output = string_replace( $output, '{{_ slug _}}', $lang->{slug} );
		$output = string_replace( $output, '{{_ label _}}', $lang->{label} );
		$output_queue .= $output . "\n";
	}
	$html = string_replace($html, '{{_ menu_items _}}', $output_queue);

	# Process columns
	$output_queue = '';
	foreach my $lang (@{$json->{languages}}) {
		$output = $column_headers_template;
		if ( array_contains( $lang->{slug}, @{$json->{default_langs}} ) ) {
			$output = string_replace($output, '{{_ hidden _}}', '');
		} else {
			$output = string_replace($output, '{{_ hidden _}}', 'hidden');
		}
		$output = string_replace( $output, '{{_ slug _}}', $lang->{slug} );
		$output = string_replace( $output, '{{_ label _}}', $lang->{label} );
		$output_queue .= $output . "\n";
	}
	$html = string_replace($html, '{{_ column_headers _}}', $output_queue);
}

# Process codes
if ( $json->{codes} ) {
	$output_queue = '';
	foreach my $code (@{$json->{codes}}) {
		$output = $code_rows_template;
		$output = string_replace( $output, '{{_ code_label _}}', $code->{label} );
		# Code cells
		$output_queue2 = '';
		foreach my $lang (@{$json->{languages}}) {
			$output2 = $code_cell_template;
			if ( array_contains( $lang->{slug}, @{$json->{default_langs}} ) ) {
				$output2 = string_replace($output2, '{{_ hidden _}}', '');
			} else {
				$output2 = string_replace($output2, '{{_ hidden _}}', 'hidden');
			}
			$output2 = string_replace( $output2, '{{_ slug _}}', $lang->{slug} );
			$output2 = string_replace( $output2, '{{_ label _}}', $lang->{label} );
			$output2 = string_replace( $output2, '{{_ code _}}', get_file_contents('src/codes/' . $lang->{slug} . '/' . $code->{slug} . '.' . $lang->{ext}) );
			$output_queue2 .= $output2;
		}
		$output = string_replace( $output, '{{_ code_cells _}}', $output_queue2 );
		$output_queue .= $output . "\n";
	}
	$html = string_replace($html, '{{_ code_rows _}}', $output_queue);
}

# Write output
write_file_contents($html, 'public/index.html');
